package com.wust.footprint;

import java.util.HashMap;
import java.util.Map;



import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import com.google.gson.Gson;

import com.wust.R;
import com.wust.http.HttpUtil;
import com.wust.menu.MainActivity;
import com.wust.user.User;
import com.wust.user.UserDao;

public class LoginActivity extends Activity{
		private EditText usertext;
		private EditText passtext;
		private CheckBox savechb;
		private ImageButton loginbtn;
		private ImageButton regbtn;
		private String username;
		private String password;
		private boolean issave=false;
		private int haslogin=1;
		@Override
		protected void onCreate(Bundle savedInstanceState) {
			// TODO Auto-generated method stub
			super.onCreate(savedInstanceState);
			setContentView(R.layout.loginpage);
			usertext=(EditText)findViewById(R.id.login_edit_account);
			passtext=(EditText)findViewById(R.id.login_edit_pwd);
			savechb=(CheckBox)findViewById(R.id.login_cb_savepwd);
			loginbtn=(ImageButton)findViewById(R.id.login_bt);
			regbtn=(ImageButton)findViewById(R.id.regist_bt);
			loginbtn.setOnClickListener(new LoginListener());
			regbtn.setOnClickListener(new RegisterListener());
			savechb.setOnCheckedChangeListener(new CheckChangeListener());
		}
		 @Override
			protected Dialog onCreateDialog(int id) {
		    	System.out.println("dialog");
				ProgressDialog dialog = new ProgressDialog(this);
				dialog.setMessage("加载中...请稍候");
				dialog.setIndeterminate(true);
				dialog.setCancelable(true);
				return dialog;
			}
		 class CheckChangeListener implements OnCheckedChangeListener
		 {

			@Override
			public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
				// TODO Auto-generated method stub
				if(isChecked)
				{
					issave=true;
				}
			}

			
			 
		 }
		class LoginListener implements OnClickListener
		{

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				username=usertext.getText().toString();
				password=passtext.getText().toString();
				showDialog(0);
				new Thread()
				{
					public void run()
					{
						User userlogin=new User();
						userlogin.setUser_name(username);
				    	userlogin.setUser_pass(password);
				    	Gson gson=new Gson();
				    	System.out.println("issave:"+issave);
				    	if(issave==false)
				    	{
				    		password="";
				    		haslogin=0;
				    	}
				    	Map<String,String>map=new HashMap<String,String>();
						map.put("json", gson.toJson(userlogin));
						String result=null;
						try {
							result = HttpUtil.postRequest(com.wust.util.ConstDefine.URL+"loginaction",map);
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						userlogin=gson.fromJson(result, User.class);
						UserDao userDao=new UserDao(LoginActivity.this);
						if(userlogin!=null)
						{
							User user=userDao.getUserByName(username);
							if(user.getUser_id()>0)
							{
							    if(user.getUser_pass().equals(userlogin.getUser_pass()))
								{
									if(user.getUser_haslogin()==1)
									{
										Intent intent=new Intent(LoginActivity.this,MainActivity.class);
										startActivity(intent);
										finish();
									}
									else
									{
										User temp=userDao.checkUserHasLogin();
										if(temp!=null)
										{
										temp.setUser_haslogin(0);
										userDao.updateUser(temp);
										}
										user.setUser_haslogin(1);
										user.setUser_pass(password);
										userDao.updateUser(user);
										Intent intent=new Intent(LoginActivity.this,MainActivity.class);
										startActivity(intent);
										finish();
									}
								}
							    else
							    {
							    	if(user.getUser_haslogin()==1)
									{
							    		user.setUser_pass(password);
							    		userDao.updateUser(user);
										Intent intent=new Intent(LoginActivity.this,MainActivity.class);
										startActivity(intent);
										finish();
									}
									else
									{
										User temp=userDao.checkUserHasLogin();
										if(temp!=null)
										{
										temp.setUser_haslogin(0);
										userDao.updateUser(temp);
										}
										user.setUser_pass(password);
										user.setUser_haslogin(1);
										userDao.updateUser(user);
										Intent intent=new Intent(LoginActivity.this,MainActivity.class);
										startActivity(intent);
										finish();
									}
							    }
						}
							else
							{
								User temp=userDao.checkUserHasLogin();
								if(temp!=null)
								{
								temp.setUser_haslogin(0);
								userDao.updateUser(temp);
								}
								userlogin.setUser_haslogin(1);
								userlogin.setUser_pass(password);
								System.out.println(userlogin);
								userDao.insertUser(userlogin);
								Intent intent=new Intent(LoginActivity.this,MainActivity.class);
								startActivity(intent);
								finish();
							}							
						}
						else
						{
							Toast.makeText(LoginActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
						}	
					}
				}.start();
			}	
			
		}
		//Register
		
		
		class RegisterListener implements OnClickListener
		{

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent=new Intent(LoginActivity.this,RegisterActivity.class);
				startActivity(intent);
			}
			
		}
}
